<template>
  <div class="category">
    <h1>{{ id ? "编辑" : "新建" }}分类</h1>
    <el-form label-width="120px" v-model="form" @submit.native.prevent="save">
      <el-form-item label="名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="广告">
       <el-button @click="form.advertise.push({})">
            <i class="el-icon-plus"></i> 添加广告</el-button
          >
          <el-row type="flex" style="flex-wrap: wrap">
            <el-col :md="24" v-for="(item, index) in form.advertise" :key="index">
              <el-form-item label="链接">
                <el-input v-model="item.url"></el-input>
              </el-form-item>
              <el-form-item label="图片" style="margin-top: 0.5rem">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL + '/upload'"
                  :show-file-list="false"
                  :on-success="(res) => $set(item, 'image', res.url)"
                >
                  <img v-if="item.image" :src="item.image" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item>
                <el-button type="danger" @click="form.skills.splice(index, 1)"
                  >删除</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
/* eslint-disable */
export default {
  name: "AdvertiseEdit",
  props: {
    id: {},
  },
  data() {
    return {
      form: {
        name: "",
        advertise: []
      },
      parentOptions: [], // 父级选项
    };
  },
  created() {
    this.id && this.edit(); // 判断是否有id,有则执行后面的方法
    this.getParentOptions();
  },
  mounted() {},
  methods: {
    // 获得分类列表
    save() {
      let res;
      if (this.id) {
        // 如果存在id，则调用编辑的接口
        res = this.$http.put(`rest/advertise/${this.id}`, this.form); // 调用这个接口
      } else {
        res = this.$http.post("rest/advertise", this.form); // 调用这个接口
      }
      this.$router.push("/advertises/list"); // 路由跳转
      this.$message({
        type: "success",
        message: "保存成功！！！",
      });
    },
    // 获得详情
    async edit() {
      const res = await this.$http.get(`rest/advertise/${this.id}`); // 调用接口
      this.form = Object.assign({}, this.form , res.data )
    },
    // 获得详情
    async getParentOptions() {
      const res = await this.$http.get(`rest/advertise`); // 调用接口
      this.parentOptions = res.data; // 将查询到的数据返回到页面
    },
  },
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  min-width: 5rem;
  height: 5rem;
  line-height: 5rem;
  text-align: center;
}
.avatar {
  min-width: 5rem;
  height: 5rem;
  display: block;
}
</style>
